package com.lq.ecoldchain.ui.main.orderlist.order.list

import android.arch.lifecycle.MutableLiveData
import com.alibaba.android.arouter.launcher.ARouter
import com.lq.ecoldchain.base.BaseNavigator
import com.lq.ecoldchain.base.viewmodel.BaseViewModel
import com.lq.ecoldchain.entity.OrderDetailBean
import com.lq.ecoldchain.entity.TakeOrderListEntity
import com.lq.ecoldchain.internet.BaseObserver
import com.lq.ecoldchain.util.toast

/*
*2019/1/17 0017  11:35
*function by lq
*/
class MineOrderViewModel:BaseViewModel(){
    val page by lazy { MutableLiveData<Int>() }
    val limit by lazy { MutableLiveData<Int>() }
    val refreshing by lazy { MutableLiveData<Boolean>() }
    val list by lazy { ArrayList<OrderDetailBean>() }
    val adapter by lazy { MineOrderListAdapter(list) }
    lateinit var state: String
    init {
        page.value =1
        limit.value =10
        adapter.setOnItemChildClickListener { _, _, position ->
          when(list[position].status){
              2->{//订单装卸货处理
                  ARouter.getInstance().build(BaseNavigator.handleOrderDetail)
                          .withString("id","${list[position].id}")
                          .navigation()
              }
              5->{
                  ARouter.getInstance().build(BaseNavigator.handleOrderDetail)
                          .withString("id","${list[position].id}")
                          .navigation()
              }
              4->{//待付款
                  ARouter.getInstance().build(BaseNavigator.orderPay).withString("id","${list[position].id}").navigation()
              }

              8->{//待客服确认送达
                  ARouter.getInstance().build(BaseNavigator.orderOver).withString("id","${list[position].id}").navigation()
              }
          }
        }
    }

    fun refreshData(){
        refreshing.value = true
        orderList()
    }

    fun orderList(){
        BaseNetCore.ApiServiceImpl.orderListTaking(state,page.value!!,limit.value!!).compose(RxTransformer.switchSchedulers()).subscribe(object:BaseObserver<TakeOrderListEntity>(){
            override fun onSuccess(t: TakeOrderListEntity?) {
                if(t!=null && !t.list.isNullOrEmpty()){
                    list.clear()
                    list.addAll(t.list)
                    adapter.notifyDataSetChanged()
                }
                if(refreshing.value==true)
                    toast { "刷新成功" }
                refreshing.value = false
            }

            override fun onFail(code: String) {
                refreshing.value = false
            }
        })
    }
}